package com.ftg.learn.parseXml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author 布衣
 */
public class JavaParseXml {

    /**
     * xml 配置文件
     *dom4j是一个Java的XML API，是jdom的升级品，用来读写XML文件的。
     * dom4j是一个十分优秀的JavaXML API，具有性能优异、功能强大和极其易使用的特点
     *
     * 1 ： 依赖
     *
     */
    public static void main(String[] args) {
        //dom4j  jdom saxreader

        String path = "D:\\javaworkspace\\qida-framework\\qida-framework\\src\\main\\resources\\teacher.xml";

        String sql = "";

        SAXReader saxReader = new SAXReader();
        try {
            Document doc = saxReader.read(new File(path));

            Element root = doc.getRootElement();
            Attribute namespace = root.attribute("namespace");
            System.out.println(namespace.getValue());

            List<Element> elements = root.elements();

            for (Element element : elements) {
                if (element.attribute("id").getValue().equals("selectTeacher")) {
                    sql = element.getText();
                }
            }
            //王天成 100W 师生 康加修 反射
            //String.class   Class.forName(bean)
            String bean = "com.ftg.learn.parseXml.SQL";
            try {
                Class sClass = Class.forName(bean);
                Object obj = sClass.newInstance();
                Method method = sClass.getMethod("setSql", String.class);
                method.invoke(obj,sql);

                System.out.println(obj);
                SQL sq = (SQL) obj;
                System.out.println(sq.getSql()+"===>");

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }

            //sClass 是com.ftg.learn.parseXml.SQL 关联对象

        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }




        //解析 path 拿到的
    }

}
